package com.dzt.androidframework.activity;

import android.Manifest;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.dzt.androidframework.JsDemo.JsActivity;
import com.dzt.androidframework.R;
import com.dzt.androidframework.adapter.FunctionAdapter;
import com.dzt.androidframework.adapter.MenuAdapter;
import com.dzt.androidframework.animation.AnimationActivity;
import com.dzt.androidframework.bean.MenuBean;
import com.dzt.androidframework.changeskin.SkinActivity;
import com.dzt.androidframework.database.DatabaseActivity;
import com.dzt.androidframework.express.ExpressActivity;
import com.dzt.androidframework.gesture.GestureActivity;
import com.dzt.androidframework.json.JsonActivity;
import com.dzt.androidframework.jsoup.JsoupActivity;
import com.dzt.androidframework.network.NetworkActivity;
import com.dzt.androidframework.permission.PermissionListener;
import com.dzt.androidframework.photo.PhotoActivity;
import com.dzt.androidframework.rxjava.RxJavaActivity;
import com.dzt.androidframework.rxpermission.PermissionActivity;
import com.dzt.androidframework.threadpool.ThreadPoolActivity;
import com.dzt.androidframework.utils.JLogKit;
import com.dzt.androidframework.utils.JToastKit;
import com.dzt.androidframework.view.ViewActivity;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ActivityTitle {
	@ViewInject(R.id.lv_main)
	private ListView lvMain;
	@ViewInject(R.id.lv_left)
	private ListView lvLeft;
	@ViewInject(R.id.dl_content)
	private DrawerLayout dlContent;
	private MenuAdapter menuAdapter;
	private List<MenuBean> datas = new ArrayList<>();
	private FunctionAdapter functionAdapter;
	private List<String> mainList = new ArrayList<>();
	// 点击退出时记录时间
	private long firstTime = 0;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_main;
	}

	@Override
	protected void initParams() {
		//requestRuntimePermission();
		titleView.setResource(R.drawable.selector_menu,
				R.string.app_name);
		titleView.setLeftClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				drawerLayoutSwitch(true);
			}
		});

		lvMain.setAdapter(functionAdapter);
		lvMain.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
				mainListItemClick(i);
			}
		});

		lvLeft.setAdapter(menuAdapter);
		lvLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
				listviewItemClick(i);
			}
		});
	}

	@Override
	protected void initData(Bundle savedInstanceState) {
		String[] menus = getResources().getStringArray(R.array.main_menus);
		for (String menu : menus) {
			mainList.add(menu);
		}
		functionAdapter = new FunctionAdapter(this, mainList);
		menuAdapter = new MenuAdapter(this, datas);
		loadData();
	}

	@Override
	protected String[] initPermissions() {
		return new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
				Manifest.permission.WRITE_EXTERNAL_STORAGE,
				Manifest.permission.READ_PHONE_STATE,
				Manifest.permission.ACCESS_COARSE_LOCATION,
				Manifest.permission.ACCESS_FINE_LOCATION,
				Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS};
	}

	private void mainListItemClick(int i) {
		switch (i) {
			case 0:
				startActivity(ActivityAlert.class, null);
				break;
			case 1:
				startActivity(DatabaseActivity.class, null);
				break;
			case 2:
				startActivity(PhotoActivity.class, null);
				break;
			case 3:
				startActivity(JsonActivity.class, null);
				break;
			case 4:
				startActivity(SkinActivity.class, null);
				break;
			case 5:
				startActivity(GestureActivity.class, null);
				break;
			case 6:
				startActivity(AnimationActivity.class, null);
				break;
			case 7:
				startActivity(JsoupActivity.class, null);
				break;
			case 8:
				startActivity(JsActivity.class, null);
				break;
			case 9:
				startActivity(ExpressActivity.class, null);
				break;
			case 10:
				startActivity(ViewActivity.class, null);
				break;
			case 11:
				startActivity(NetworkActivity.class, null);
				break;
			case 12:
				startActivity(ThreadPoolActivity.class, null);
				break;
			case 13:
				startActivity(PermissionActivity.class, null);
				break;
			case 14:
				startActivity(RxJavaActivity.class, null);
				break;
			default:
				break;
		}
	}

	private void listviewItemClick(int pos) {
		switch (pos) {
			case 0:
				startActivity(ActivitySetting.class, null);
				break;
			case 1:
				break;
			default:
				break;
		}
	}

	private void loadData() {
		String[] menus = getResources().getStringArray(R.array.menus);
		datas.clear();
		for (int i = 0; i < menus.length; i++) {
			MenuBean bean = new MenuBean();
			bean.setName(menus[i]);
			datas.add(bean);
			if (i == 0) {

			} else if (i == 1) {

			}
		}
		menuAdapter.notifyDataSetChanged();
	}

	public void drawerLayoutSwitch(boolean isOpen) {
		if (isOpen) {
			dlContent.openDrawer(Gravity.START);
		} else {
			dlContent.closeDrawer(Gravity.START);
		}
	}

	private void requestRuntimePermission() {
		requestRuntimePermission(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
						Manifest.permission.WRITE_EXTERNAL_STORAGE,
						Manifest.permission.READ_PHONE_STATE,
						Manifest.permission.ACCESS_COARSE_LOCATION,
						Manifest.permission.ACCESS_FINE_LOCATION,
						Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS},
				new PermissionListener() {

					@Override
					public void onGranted() {
						//当权限被授予时调用
						JToastKit.getInstance().showMessage("Permission granted");
					}

					@Override
					public void onDenied() {
						JToastKit.getInstance().showMessage("Permission denied");
					}

					@Override
					public void onShowRationale(String[] permissions) {
						if (permissions != null) {
							JLogKit.getInstance().i("onShowRationale");
							helper.setIsPositive(true);
							helper.request();
						}
					}
				});
	}

	@Event(value = {R.id.iv_login_logo, R.id.tv_login_name})
	private void onViewClick(View v) {
		switch (v.getId()) {
			case R.id.iv_login_logo:
			case R.id.tv_login_name:
				JLogKit.getInstance().i("click logo or name");
//				BmobUser bu = BmobUser.getCurrentUser();
//				if (bu == null) {
//					startActivity(ActivityLogin.class);
//				} else {
//					LocalApplication.userName = bu.getUsername();
//					tvLoginName.setText(LocalApplication.userName);
//					ToastUtil.getInstance(context).showMessage("登录成功");
//				}
				startActivity(ActivityLogin.class, null);
				break;
			default:
				break;
		}
	}

	@Override
	public void onBackPressed() {
		if (dlContent.isDrawerOpen(Gravity.START)) {
			drawerLayoutSwitch(false);
		} else {
			long secondTime = System.currentTimeMillis();
			// 如果两次按键时间间隔大于1000毫秒，则不退出
			if (secondTime - firstTime > 1000) {
				JToastKit.getInstance().showMessage("再按一次退出客户端");
				firstTime = secondTime;// 更新firstTime
			} else {
				super.onBackPressed();
			}
		}
	}

}
